package com.sgb.trip.viewmodel;

import android.app.Application;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.sgb.link.entity.PickSelectEntity;
import com.sgb.trip.api.TripNet;
import com.sgb.trip.entity.FlightInfoEntity;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.common.base.BaseViewModel;
import com.swgk.core.dialog.DialogHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;

/**
 * Time:2021/9/13
 * Author:黄淋
 * Description:
 */
public class FlightSearchViewModel extends BaseViewModel {
    public MutableLiveData<List<FlightInfoEntity>> ldData = new MutableLiveData<>();
    public MutableLiveData<List<PickSelectEntity>> ldDataAir = new MutableLiveData<>();
    public MutableLiveData<List<PickSelectEntity>> ldDataPeopleCount = new MutableLiveData<>();
    public MutableLiveData<String> ldToast = new MutableLiveData<>();

    public FlightSearchViewModel(@NonNull Application application) {
        super(application);
    }

    /**
     * 加载乘客人数数据
     */
    public void setPeopleCountData() {
        PickSelectEntity entity;
        List<PickSelectEntity> listData = new ArrayList<>();
        for (int i = 1; i < 10; i++) {
            entity = new PickSelectEntity();
            entity.setName(i + "人");
            entity.setValue(String.valueOf(i));
            listData.add(entity);
        }
        entity = new PickSelectEntity();
        entity.setName("不限");
        entity.setValue("");
        listData.add(0, entity);
        ldDataPeopleCount.postValue(listData);
    }

    /**
     * 搜索航班
     */
    public void checkFlight(Map<String, Object> map) {
        TripNet.getInstance().getService().checkFlight(map).enqueue(new ICallBack<BaseEntity<List<FlightInfoEntity>>>(false) {
            @Override
            public void onSuccess(BaseEntity<List<FlightInfoEntity>> data) {
                DialogHelper.dismissProgressDialog();
                if (data == null) {
                    return;
                }

                if (data.getData() != null) {
                    ldData.postValue(data.getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<FlightInfoEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                if (!TextUtils.isEmpty(t.getMessage()))
                    ldToast.postValue(t.getMessage());
            }
        });
    }

    /**
     * 航司代码
     */
    public void getCodeList() {
        Map<String, Object> map = new HashMap<>();
        map.put("isInternal", 1);
        TripNet.getInstance().getService().getCodeList(map).enqueue(new ICallBack<BaseEntity<List<PickSelectEntity>>>(false) {
            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity>> data) {
                if (data == null) {
                    return;
                }

                if (data.getData() != null) {
                    ldDataAir.postValue(data.getData());
                }
            }

            @Override
            public void onFailureWithEntity(BaseEntity entity) {

            }
        });
    }
}
